package org.jaaksi.demo.ptr.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import com.zhy.http.okhttp.OkHttpUtils;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.MaterialHeader;
import org.jaaksi.demo.ptr.R;
import org.jaaksi.demo.ptr.adapter.BaseListAdapter;
import org.jaaksi.demo.ptr.adapter.HouseAdapter;
import org.jaaksi.demo.ptr.bean.house.HouseListBean;
import org.jaaksi.demo.ptr.bean.house.HouseResponse;
import org.jaaksi.demo.ptr.ui.base.BaseListActivity;
import org.jaaksi.demo.ptr.util.DensityUtil;
import org.jaaksi.demo.ptr.util.ModuleCallback;
import org.jaaksi.demo.ptr.util.UriUtil;

/**
 * Created by Fcy on 2016/3/21.<br/>
 * Android Material风格
 */
public class MaterialActivity extends BaseListActivity<HouseListBean> {

    public static void goTo(Context context) {
        Intent intent = new Intent(context, MaterialActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_loadmore);
    }

    @Override
    protected BaseListAdapter<HouseListBean> getAdapter() {
        return new HouseAdapter(this);
    }

    @Override
    protected boolean isPinContent() {
        return true;
    }

    @Override
    protected View initPtrHeader() {
        final MaterialHeader header = new MaterialHeader(this);
        int[] colors = getResources().getIntArray(R.array.google_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        header.setPadding(0, DensityUtil.dip2px(this, 15), 0, DensityUtil.dip2px(this, 10));
        return header;
    }

    @Override
    protected void buildRequest() {
        OkHttpUtils.get()
                   // .headers().params()
                   .url(UriUtil.getUriHouseList("110000", getLoadOffset(), getPageSize()))
                   .tag(this)
                   .build()
                   .execute(new ModuleCallback<HouseResponse>(HouseResponse.class) {
                       @Override
                       public void onResponse(HouseResponse response) {
                           if (response != null && response.errno == 0 && response.data != null &&
                                   response.data.resblock_list !=
                                           null) { // 如果使用服务器hasMore字段，则调用setDatas两个参数的方法，
                               // 否则要调用initTotalPages设置总个数，且调用一个参数的方法
                               // initTotalPages(
                               // response.data.resblock_list.total_count);
                               setDatas(response.data.resblock_list.list,
                                       response.data.resblock_list.has_more_data == 1);
                           } else { // 如果list=null，就设置为null,true
                               setDatas(null, true);
                           }
                       }
                   });

    }
}
